<?php
//header('Content-Type: text/html; charset=utf-8');
require_once("../sqlConn.php");
require_once("../data/common_define.php");

$action=$_GET['action'];

//$action='allReqList';

//每页显示的记录条数
$perPageRecord=$_POST['perPageRecord'];
//页码
$pageNo=$_POST['pageNo'];
//查询的起始条数
$startNo=$pageNo*$perPageRecord;
$limitSql=" limit $startNo,$perPageRecord";

$lastNO=0;
$lastSql = "limit $lastNO,1";
switch($action)
{
	case 'iReqList':
	    $userID=$_POST['userID'];
		//$userID=1;
		$countSql="select count(*) from help where userID='$userID'";
	    $sql="select * from help where userID='$userID' order by id desc ".$limitSql;  
		GetReqList($countSql,$sql);
		break;


	case 'allReqList':
	    $countSql="select count(*) from help";
	    $sql="select * from help order by id desc ".$limitSql;
		GetReqList($countSql,$sql);
		break;

	case 'iApplyList':
	    $userID=$_POST['userID'];		
	    $countSql="select count(*) from help left join apply a on a.helpID=id  where a.uID='$userID' ";
		$sql="select * from help left join apply a on a.helpID=id  where a.uID='$userID' order by id desc ".$limitSql; 
		GetReqList($countSql,$sql);
		break;

	case 'iChooseList':
	    $userID=$_POST['userID'];
		//echo "userID = ".$userID;
		$countSql="select count(*) from help where chooseID='$userID' ";
		$sql="select * from help where chooseID='$userID' order by id desc ".$limitSql; 
		GetReqList($countSql,$sql);
		break;

	case 'iFocusList':
	    $userID=$_POST['userID'];
		$countSql="select count(*) from help as h LEFT JOIN focuscate as f ON (f.cateID=h.cateid) where f.userID='$userID' ";
		$sql="select h.* from help as h LEFT JOIN focuscate as f ON (f.cateID=h.cateid) where f.userID='$userID' order by id desc ".$limitSql;
		GetReqList($countSql,$sql);
		break;

	case 'friendReqList':
	    $userID=$_POST['userID'];
		$countSql="select count(*) from help as h LEFT JOIN friend as f ON (f.frid=h.userID) where f.foid='$userID' ";
		$sql="select h.* from help as h LEFT JOIN friend as f ON (f.frid=h.userID) where f.foid='$userID' order by id desc ".$limitSql;
		GetReqList($countSql,$sql);
		break;

	case 'cateReqList':
	    $cate=$_POST['cate'];
		$countSql="select count(*) from help where cateid='$cate' "; 
		$sql="select * from help where cateid='$cate' order by id desc ".$limitSql; 
		GetReqList($countSql,$sql);
		break;
	
	case 'lastReq':
	    $userID=$_POST['userID'];
		$countSql="select count(*) from help where userID='$userID'";
	    $sql="select * from help where userID='$userID' order by id desc ".$lastSql;  
		GetReqList($countSql,$sql);
		break;
		
}

function GetReqList($countSql,$sql)
{
    global $cateArr;
	global $statusArr;
	
	connectDB();
	
	$countRes=mysql_query($countSql);
	$countRow=mysql_fetch_array($countRes);
	$reqCount=$countRow[0];
	
	$res=mysql_query($sql);	
	$allReqList="[";
	$i=0;	
	while($row=mysql_fetch_array($res))
	{	
		//将textarea中上传的文本中的换行符替换为"<br>"
		$description=str_replace("\r\n","<br>",$row['description']);
		$description=str_replace("\n","<br>",$description);
		$detail=str_replace("\r\n","<br>",$row['detail']);
		$detail=str_replace("\n","<br>",$detail);
		
		$cateid=$row["cateid"];//
		$cate = $cateArr["$cateid"];
		$rowStr="{id:'".$row["id"]."',setuptime:'".$row["setuptime"]."',title:'".$row["title"]."',money:'".$row["money"]."',userID:'".$row["userID"]."',requesterName:'".$row["requesterName"]."',cate:'".$cate."',description:'".$description."',detail:'".$detail."',status:'".$row["status"]."',statusstr:'".$statusArr[$row["status"]]."',chooseID:'".$row["chooseID"]."',chooseName:'".$row["chooseName"]."',flagScoreReq:'".$row["flagScoreReq"]."',ScoreReq:'".$row["ScoreReq"]."',flagScoreChoose:'".$row["flagScoreChoose"]."',ScoreChoose:'".$row["ScoreChoose"]."'},";
		$allReqList.=$rowStr;		
		$i++;
	}
	if($i>0)
	{
		$allReqList=substr_replace($allReqList,'',-1,1);
	}
	$allReqList.="]";
	
	//echo $allReqList;
	
	$reqObj="{reqCount:'".$reqCount."',reqList:".$allReqList."}";
	
	echo $reqObj;
}

?>